﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml;
using System.Threading;
using System.Configuration;
using System.Text;
using System.Windows.Forms;
using DBAccessLib;

namespace LineWatchSimple.WinForm
{
    public partial class ProductNumSetView : Form
    {
        public ProductManagementView ProductManagementForm;
        private DataTable dtProductInfo = new DataTable();

        public ProductNumSetView()
        {
            InitializeComponent();
        }


        private void ProductNumSetView_Load(object sender, EventArgs e)
        {
            this.InitData();
        }

        private void InitData()
        {
            string NumTime1 = LineShift.GetLineNumTime(ConfigInfo.LineCode1);
            ConfigInfo.DtLineShift1 = LineShift.GetLineShift_New(ConfigInfo.LineCode1, NumTime1);
            //DateTime currentWorkDate1 = ConfigInfo.GetCurrentWorkDate(ConfigInfo.DtLineShift1);
            string shift1 = ConfigInfo.GetShift(ConfigInfo.DtLineShift1);
            ConfigInfo.DtProductInfo1 = ProductInfo.GetProductInfo(ConfigInfo.LineCode1, shift1, NumTime1);

            string NumTime2 = LineShift.GetLineNumTime(ConfigInfo.LineCode2);
            ConfigInfo.DtLineShift2 = LineShift.GetLineShift_New(ConfigInfo.LineCode2, NumTime2);
            //DateTime currentWorkDate2 = ConfigInfo.GetCurrentWorkDate(ConfigInfo.DtLineShift2);
            string shift2 = ConfigInfo.GetShift(ConfigInfo.DtLineShift2);
            ConfigInfo.DtProductInfo2 = ProductInfo.GetProductInfo(ConfigInfo.LineCode2, shift2, NumTime2);

            string NumTime3 = LineShift.GetLineNumTime(ConfigInfo.LineCode3);
            ConfigInfo.DtLineShift3 = LineShift.GetLineShift_New(ConfigInfo.LineCode3, NumTime3);
            //DateTime currentWorkDate3 = ConfigInfo.GetCurrentWorkDate(ConfigInfo.DtLineShift3);
            string shift3 = ConfigInfo.GetShift(ConfigInfo.DtLineShift3);
            ConfigInfo.DtProductInfo3 = ProductInfo.GetProductInfo(ConfigInfo.LineCode3, shift3, NumTime3);

            this.dtProductInfo = ConfigInfo.DtProductInfo1.Copy();
            this.dtProductInfo.Merge(ConfigInfo.DtProductInfo2);
            this.dtProductInfo.Merge(ConfigInfo.DtProductInfo3);

            this.dataGridView3.DataSource = this.dtProductInfo;
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            this.dataGridView3.EndEdit();

            foreach (DataRow row in dtProductInfo.Rows)
            {
                if (string.Equals(row["LineCode"].ToString().Trim(), ConfigInfo.LineCode1))
                {
                    string NumTime1 = LineShift.GetLineNumTime(ConfigInfo.LineCode1);
                    ConfigInfo.DtLineShift1 = LineShift.GetLineShift_New(ConfigInfo.LineCode1, NumTime1);
                   // DateTime currentWorkDate = ConfigInfo.GetCurrentWorkDate(ConfigInfo.DtLineShift1);
                    string shift = ConfigInfo.GetShift(ConfigInfo.DtLineShift1);

                    row["Shift"] = shift;
                }
                else if (string.Equals(row["LineCode"].ToString().Trim(), ConfigInfo.LineCode2))
                {
                    string NumTime2 = LineShift.GetLineNumTime(ConfigInfo.LineCode2);
                    ConfigInfo.DtLineShift2 = LineShift.GetLineShift_New(ConfigInfo.LineCode2, NumTime2);
                    //DateTime currentWorkDate = ConfigInfo.GetCurrentWorkDate(ConfigInfo.DtLineShift2);
                    string shift = ConfigInfo.GetShift(ConfigInfo.DtLineShift2);

                    row["Shift"] = shift;
                }
                else if (string.Equals(row["LineCode"].ToString().Trim(), ConfigInfo.LineCode3))
                {
                    string NumTime3 = LineShift.GetLineNumTime(ConfigInfo.LineCode3);
                    ConfigInfo.DtLineShift3 = LineShift.GetLineShift_New(ConfigInfo.LineCode3, NumTime3);
                    //DateTime currentWorkDate = ConfigInfo.GetCurrentWorkDate(ConfigInfo.DtLineShift3);
                    string shift = ConfigInfo.GetShift(ConfigInfo.DtLineShift3);

                    row["Shift"] = shift;
                }
            }

            bool result = ProductInfo.UpdateProductInfo(this.dtProductInfo);

            if (this.ProductManagementForm != null)
            {
                this.ProductManagementForm.GetProductInfoAndUpdateUI();
            }

            if (result)
            {
                MessageBox.Show("保存成功!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("保存失败!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.InitData();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int RowNumber;
                if (null == dataGridView3.CurrentCell)
                {
                    return;
                }
                RowNumber = dataGridView3.CurrentCell.RowIndex;
                dataGridView3.Rows.RemoveAt(RowNumber);
                bool result = ProductInfo.UpdateProductInfo(this.dtProductInfo);
                if (!result)
                {
                    MessageBox.Show("删除失败！！！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败！！！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
                ConfigurationManager.AppSettings["NowPlanA"] = textBox1.Text.ToString().Trim();
                ConfigurationManager.AppSettings["NowPlanTimeA"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ConfigurationManager.AppSettings["NowPlanA"] = "0";
            ConfigurationManager.AppSettings["NowPlanTimeA"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //string t = string.Empty;
            //int outT;
            //if (!int.TryParse(t, out outT)) outT = 200;
            //Thread.Sleep(outT);
            //XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load(ConfigurationManager.AppSettings["Directory"].ToString().Trim());
            //XmlElement node1 = (XmlElement)xmlDoc.SelectSingleNode("//add[@key='NowPlanA']");
            //node1.SetAttribute("value", "0");
            //XmlElement node2 = (XmlElement)xmlDoc.SelectSingleNode("//add[@key='NowPlanTimeA']");
            //node2.SetAttribute("value", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            //xmlDoc.Save(ConfigurationManager.AppSettings["Directory"].ToString().Trim());
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ConfigurationManager.AppSettings["NowPlanB"] = textBox2.Text.ToString().Trim();
            ConfigurationManager.AppSettings["NowPlanTimeB"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ConfigurationManager.AppSettings["NowPlanB"] = "0";
            ConfigurationManager.AppSettings["NowPlanTimeB"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load(ConfigurationManager.AppSettings["Directory"].ToString().Trim());
            //XmlElement node1 = (XmlElement)xmlDoc.SelectSingleNode("//add[@key='NowPlanB']");
            //node1.SetAttribute("value", "0");
            //XmlElement node2 = (XmlElement)xmlDoc.SelectSingleNode("//add[@key='NowPlanTimeB']");
            //node2.SetAttribute("value", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            //xmlDoc.Save(ConfigurationManager.AppSettings["Directory"].ToString().Trim());
        }

        private void button7_Click(object sender, EventArgs e)
        {
            ConfigurationManager.AppSettings["NowPlanC"] = textBox3.Text.ToString().Trim();
            ConfigurationManager.AppSettings["NowPlanTimeC"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ConfigurationManager.AppSettings["NowPlanC"] = "0";
            ConfigurationManager.AppSettings["NowPlanTimeC"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //string t = string.Empty;
            //int outT;
            //if (!int.TryParse(t, out outT)) outT = 200;
            //Thread.Sleep(outT);
            //XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load(ConfigurationManager.AppSettings["Directory"].ToString().Trim());
            //XmlElement node1 = (XmlElement)xmlDoc.SelectSingleNode("//add[@key='NowPlanC']");
            //node1.SetAttribute("value", "0");
            //XmlElement node2 = (XmlElement)xmlDoc.SelectSingleNode("//add[@key='NowPlanTimeC']");
            //node2.SetAttribute("value", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            //xmlDoc.Save(ConfigurationManager.AppSettings["Directory"].ToString().Trim());
        }
    }
}
